﻿@using Jb.Web.Utility;
@{    
	String pageTitle = "Search Configuration";
    ViewBag.Title = Html.GetPageTitle(pageTitle);
    ViewData[AppConstants.ViewDataKeys.SELECTED_MENU] = AdminMenu.SearchConfig;
    //Jb.Search.ReIndexResult _Result = ViewData[AppConstants.ViewDataKeys.SAVED] as Jb.Search.ReIndexResult; 
}
<div class="jb-page-heading">
    <h1>@pageTitle</h1>
    <div>
        Manage and Configure Job and Resume search.
    </div>
</div>

@using (Html.BeginForm())
{
    <div class="jb-page-content">                
        <div class="span6">
            <div id="divResumeProgress">                
                <div>Resume Index Progress</div>
                <div class="progress">
                    <div class="bar" style="width: 0%;"></div>                
                </div>
            </div>
            <input type="button" class="btn" value="Re Index Resumes" onclick="ConfirmReIndex('R');" />
            <div id="divResumeIndexResult"></div>
        </div>
        <div class="span6">
            <div id="divJobProgress">
                <div>Job Index Progress</div>
                <div class="progress">
                    <div class="bar" style="width: 0%;"></div>
                </div>
            </div>
            <input type="button" class="btn" value="Re Index Jobs"  onclick="ConfirmReIndex('J');" />
            <div id="divJobIndexResult">sdf ads fsad fasdfadfsda</div>
        </div>
        @*<div class="clearfix"></div>*@
    </div>
    
    <!--Modal Popup Section Start-->
    <div id="divConfirmDialog" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="divPhotoEditModalLabel" aria-hidden="true">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">X</button>
            <h4 id="myModalLabel">Confirm Reindexing</h4>
        </div>
        <div class="modal-body jb-inline-label">            
            <div id="divModalMesssage"></div>
        </div>
        <div class="modal-footer">               
            <input type="button" class="btn btn-primary" onclick="ReIndexDocuments();" value="Start Re-Indexing"/>
            <button class="btn" data-dismiss="modal" aria-hidden="true"><i class="icon-off"></i> Cancel</button>
        </div>
    </div>
    <!-- Modal Popup Section End -->
}
@section PageHeadSection{
<style type="text/css">
    #divResumeIndexResult, #divJobIndexResult{margin-top:10px; display:none;}
</style>
}
@section JavaScriptPlaceHolder
{
    <script language="javascript" type="text/javascript">
        var _ReIndexingType = '';
        var _CheckStatusTimeout = 1; //Should be given in seconds
        function ConfirmReIndex(type) {
            _ReIndexingType = type;
            if (_ReIndexingType == 'R') {
                $('#divModalMesssage').html('Are you sure you want to Re-Index all active resumes? It can take quite some time, depending on number of stored resumes.');
                $('#divResumeIndexResult').html('').hide();
            }
            else if (_ReIndexingType == 'J') {
                $('#divModalMesssage').html('Are you sure you want to Re-Index all active jobs? It can take quite some time, depending on number of published jobs.');
                $('#divJobIndexResult').html('').hide();
            }
            $('#divConfirmDialog').modal({ show: true });
        }
        function ReIndexDocuments() {
            $('#divConfirmDialog').modal('hide');
            //ShowLoadingAnimation();
            EnQueueJob(_ReIndexingType);
        }
        function StartReIndexing(jobID) {
            CheckJobStatus(jobID);
            var url = _ReIndexingType == 'R' ? '@Url.AdminUrl("ReIndexResumes", "SearchConfig")' : '@Url.AdminUrl("ReIndexJobs", "SearchConfig")';
            Ajax(url, 'jobid=' + jobID, function (result) {
                if (result != null && result.length > 0) {
                    var jobStatus = eval('(' + result + ')');
                    if (_ReIndexingType == 'R')
                        BuildResult(jobStatus, 'Resumes', '#divResumeIndexResult');
                    else
                        BuildResult(jobStatus, 'Jobs', '#divJobIndexResult');
                }
            });
        }        
        function CheckJobStatus(jobID) {
            AjaxGET('@Url.PublicUrl("CheckStatus", "Ajax")?jobid=' + jobID, function (result) {
                var jobStatus = eval('(' + result + ')');
                var progressContainer = _ReIndexingType == 'R' ? $('#divResumeProgress') : $('#divJobProgress');                
                if (jobStatus.Status == '@(Jb.Search.QueueStatus.ERROR)') {
                    //alert('Got Error from Server');
                    if(_ReIndexingType == 'R')
                        BuildResult(jobStatus, 'Resumes', '#divResumeIndexResult'); 
                    else
                        BuildResult(jobStatus, 'Jobs', '#divJobIndexResult');                    
                }
                else if (jobStatus.Completed < jobStatus.Total)
                    setTimeout(function () { CheckJobStatus(jobID); }, (_CheckStatusTimeout * 1000));
                else
                    jobStatus.Total = jobStatus.Completed;
                UpdateProgressUI(jobStatus, progressContainer);
            });
        }
        //var _JobID = 0;
        function EnQueueJob(reIndexType) {
            var url = '@Url.AdminUrl("EnQueueJob", "SearchConfig")';
            AjaxGET(url + '?ReIndexType=' + reIndexType, function (result) {
                var job = eval('(' + result + ')');
                //_JobID = job.ID;
                ///Actually Start the ReIndex Operation                
                if (reIndexType == 'R')
                    UpdateProgressUI(job, $('#divResumeProgress')); //$('#divResumeIndexResult').html(result);
                else
                    UpdateProgressUI(job, $('#divJobProgress')); //$('#divJobIndexResult').html(result);
                StartReIndexing(job.ID);
            });
        }
        function UpdateProgressUI(job, divContainer) {
            $(divContainer).find('div:first').html('Indexing ' + job.Completed + ' of ' + job.Total);
            var progress = 0;
            if (job.Total == job.Completed)
                progress = 100;
            else if (job.Total > 0 && job.Completed > 0)
                progress = ((100 * job.Completed) / job.Total);

            $(divContainer).find('div.bar').css('width', progress + '%');
        }
        function BuildResult(reIndexResult, title, container) {
            var html = '';
            if (typeof (reIndexResult.Error) === 'undefined' || reIndexResult.Error.length == 0) {
                html = '<div>Deleted Index from DB: ' + reIndexResult.DeletedIndexFromDB + '</div>' +
                        '<div>Deleted Index from Lucene: ' + reIndexResult.DeletedIndexFromLucene + '</div>' +
                        '<div>Found ' + title + ' for Re-Index: ' + reIndexResult.FoundItems + '</div>' +
                        '<div>Successfully Indexed: ' + reIndexResult.ReIndexedItems + '</div>';
                html = SuccessMessageBox(html, '');
            }   
            else
                html = WarningMessageBox(reIndexResult.Error, 'Error! Following Error Occured In Server.');            
           $(container).html(html).show();
        }
    </script>
}

    


